<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+


include('includes/InterPhoto.Core.php');


$newid = ForceIncomingInt('id');

$smarty = new InterPhoto;


if($newid){
	$thisnews = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "news  WHERE actived = 1 AND newid='$newid' ");
	$DB->query("UPDATE " . TABLE_PREFIX . "news SET viewcount = (viewcount + 1) WHERE actived = 1 AND newid='$newid' ");

	if($thisnews['keywords']){
		$smarty->assign('metakeywords', $thisnews['keywords'] . ','.$mainsettings['siteKeywords']);
		$smarty->assign('metadescription', $thisnews['keywords'] . ',' .$mainsettings['siteMeta']);
	}

	$smarty->assign('thisnews', $thisnews);

}else{

	$page = ForceIncomingInt('p', 1);
	$NumPerPage = 20;
	$start = $NumPerPage * ($page-1);

	$getnews = $DB->query("SELECT newid, title, created FROM " . TABLE_PREFIX . "news  WHERE actived = 1 ORDER BY ordernum DESC LIMIT $start,$NumPerPage");
	$maxrows = $DB->query_first("SELECT COUNT(newid) AS value FROM " . TABLE_PREFIX . "news  WHERE actived = 1 ");

	$news = $DB->getrows($getnews);

	$totalpages = ceil($maxrows['value'] / $NumPerPage);

	$smarty->assign('pagelist', Iif($totalpages>1, GetPageList('news.php', $totalpages, $page, 10), ''));
	$smarty->assign('news', $news);
	$smarty->assign('start', $start);
}

$getimages = $DB->query("SELECT i.imageid, i.path, i.filename, i.title FROM " . TABLE_PREFIX . "images i LEFT JOIN  " . TABLE_PREFIX . "categories c ON (c.categoryid = i.categoryid) WHERE (i.usergroupids = 'all' OR i.usergroupids LIKE '%(".$userinfo['groupid'].")%') AND i.actived = 1 AND c.actived = 1 ORDER BY rand() LIMIT 10");

$images = $DB->getrows($getimages);

$smarty->assign('images', $images);
$pagenav = '<a href="'.GetUrl('index.php').'">'.$langs['home'].'</a> '.$langs['nav'].' <a href="'.GetUrl('news.php').'">'.$langs['webnews'].'</a>'
	.Iif($thisnews, ' '.$langs['nav'].' <span class=navtitle>'.ShortTitle($thisnews['title'], 48).'</span>');

//Always needing
$smarty->assign('pagenav', $pagenav);
$smarty->assign('userinfo', $userinfo);
$smarty->assign('pagetitle', Iif($thisnews, $thisnews['title'], $langs['webnews']) . ' - ' .$mainsettings['siteTitle']);

$smarty->interPlay('news.tpl');

?>